const express = require('express');
const path = require('path');
const cheerio = require('cheerio');
const superagent = require('superagent');

const app = express();
app.use('/node_modules', express.static(path.join(__dirname, '..', 'node_modules')));
app.use(express.static(path.join(__dirname, '..', 'public')));
app.engine('.html', require('ejs').__express);

// Optional since express defaults to CWD/views

app.set('views', path.join(__dirname, '..', 'views'));
// Without this you would need to
// supply the extension to res.render()
// ex: res.render('users.html').
app.set('view engine', 'html');

app.get('/search-agent', async (req, oRes) => {
  const keyword = req.query.keyword || 'node 实践';
  // console.log('keyword=', keyword);
  // const sortBy = req.query.sortBy || 'viewCount' // 默认按照阅读量排序
  const page = parseInt(req.query.page) || 1;
  const pageSize = 10;

  try {
    // const response = await axios.get(`https://juejin.cn/search?query=${encodeURIComponent(keyword)}&sort=${sortBy}&page=${page}`)
    superagent
      .get(`https://juejin.cn/search?query=${encodeURIComponent(keyword)}&fromSeo=0&fromHistory=0`)
      .end((err, res) => {
        // Calling the end function will send the request
        if (err) console.log('err', err);
        // console.log('res', res);
        const $ = cheerio.load(res.text);
        const articles = [];

        $('.main-list > .item').each((index, element) => {
          const title = $(element).find('.title').text().trim();
          const link = $(element).find('.title').attr('href');
          const likeCount = parseInt($(element).find('.like .count').text().trim());
          const commentCount = parseInt($(element).find('.comment .count').text().trim());
          const brief_content = $(element).find('.abstract').text().trim();
          // const collectCount = parseInt(
          //   $(element).find('.meta-list .iconfont-collection').text().trim()
          // );

          articles.push({
            title,
            link,
            commentCount,
            likeCount,
            brief_content
            // collectCount
          });
        });

        const startIndex = (page - 1) * pageSize;
        const endIndex = startIndex + pageSize;
        const paginatedArticles = articles.slice(startIndex, endIndex);
        console.log('articles', articles);

        // res.json(paginatedArticles);
        oRes.render('search-agent', {
          data: paginatedArticles,
          title: 'juejin search agent',
          header: 'search-agent'
        });
      });
  } catch (error) {
    console.error('Error:', error);
    oRes.status(500).json({ error: 'An error occurred' });
  }
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
